var id = getQueryString("activity_id")
var duankou
var kehu
var pingtai
var text;
var ordinary_url;
var contract_url;
var common = 79 //要限制输入的字数
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
$(function () {
    var userData={
        activity_id: id
    }
    $.ajax({
        type: "post",
        url: root.sroreinter.search_activity_conf,
        cache: false,  //禁用缓存
        dataType: 'json',
        data: userData,
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data[0].rich_text;
                ue.ready(function() {
                    ue.setContent(dataInter);
                });

                $("#title").val(data.data[0].title);
                var kk=$("#title").val()
                var ss= kk.replace(/[\u0391-\uFFE5]/g,"aa").length;
                $("#textSpan").html(ss)
                $("#logmin").val(data.data[0].started_at);
                $("#logmax").val(data.data[0].end_in);
                $("#ordinary_url").val(data.data[0].url_path_ordinary);
                $("#contract_url").val(data.data[0].url_path);
                ordinary_url=data.data[0].url_path_ordinary
                contract_url=data.data[0].url_path
                code=data.data[0].warehouse_code;
                code=code.split(',')
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
                for(var i=0;i<code.length;i++){//id相同为选中状态
                    console.log(code[i])
                    if(code.indexOf(code[i])>= 0){
                        $.each(warehouses.data,function(k,s){
                            if(code[i]==s.warehouseCode){
                                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                                warecode.checked=true
                            }

                        })

                    }
                }
                if(code.indexOf("49") >= 0 ) {
                    $("#jiangsu").attr("checked","checked")
                }
                if(code.indexOf("50") >= 0 ) {
                    $("#wuhan").attr("checked","checked")
                }
                code=data.data[0].warehouse_code;
                CangName=data.data[0].warehouse_name;
                pingtai=data.data[0].platform;
                if (pingtai=='10'){
                    $("#kehuleixing").removeAttr("style");
                    $("#qianyue").attr("checked","checked")
                }else if (pingtai=='20'){
                    $("#kehuleixing").attr("style", "display:none");
                    $("#putong").attr("checked","checked")
                }else if (pingtai=='10,20'){
                    $("#kehuleixing").removeAttr("style");
                    $("#putong").attr("checked","checked")
                    $("#qianyue").attr("checked","checked")
                }
                duankou=data.data[0].port;
                if (duankou=='10'){
                    $("#pc").attr("checked","checked")
                    common=109
                }else if (duankou=='20'){
                    $("#yidong").attr("checked","checked")
                    common=79
                }
                $('#commonText').text('('+common+')')
                kehu=data.data[0].different_customer_code;
                if (kehu=='10'){
                    $("#fen").attr("checked","checked")
                }else if (kehu=='20'){
                    $("#nofen").attr("checked","checked")
                }else if (kehu=='10,20'){
                    $("#fen").attr("checked","checked")
                    $("#nofen").attr("checked","checked")
                }
            }
        }
    })
})

$("#form-member").validate({
    rules: {},
    //提交表单数据
    submitHandler:function(form){

        //循环class，获取到每个check的选中状态
        if (duankou=='0'){
            $("#yidong").css('outline','1px solid red')
            $("#pc").css('outline','1px solid red')
            layer.msg("请配置端口", {icon: 6, time: 3000});
            return false
        }
        if (pingtai=='0'){
            $("#putong").css('outline','1px solid red')
            $("#qianyue").css('outline','1px solid red')
            layer.msg("请配置平台", {icon: 6, time: 3000});
            return false
        }
        if(pingtai=='10'||pingtai=='10,20'){
            if (kehu=='0'){
                $("#fen").css('outline','1px solid red')
                $("#nofen").css('outline','1px solid red')
                layer.msg("请配置签约平台下的客户类型", {icon: 6, time: 3000});
                return false
            }
        }
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red')
            })
            layer.msg("请配置区域", {icon: 6, time: 3000});
            return false
        }
        ue.ready(function() {
            text = ue.getContent();
        });
        //提交
        var user_data = {
            port : duankou,
            different_customer_code : kehu,
            platform : pingtai,
            warehouse_code : code,
            warehouse_name : CangName,
            title : $("#title").val(),
            started_at : $("#logmin").val(),
            end_in : $("#logmax").val(),
            rich_text : text,
            activity_id : id,
            url_path:$("#contract_url").val(),
            url_path_ordinary:$("#ordinary_url").val(),
            last_modified_name:JSON.parse(web.readCookie("loginData")).user.username,
            last_modified_by:JSON.parse(web.readCookie("loginData")).user.user_ID
        };
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data);
            if (data.errorCode=="92"){
                layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
            }
            if(data.errorCode=="0") {
                layer.msg('操作成功!', {icon: 6, time: 1000});
                parent.queryDataFn(parent.comPageNum);
                setTimeout( function () {
                    cancelLayerNoRefreshFn();
                },700);
            }
            if (data.errorCode=="93"){
                layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                return
            }
        }
        //给一个接口地址提交数据
        return web.doAjax(root.sroreinter.save_activity_conf, 'post', user_data, succCallBack);
    }
})

function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        /* var uu= document.getElementById('ying'+s.warehouseCode+'')
          uu.css('outline','')*/
        if(document.getElementById('ying'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('ying'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('ying'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename




}

function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#kehuleixing").removeAttr("style");
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#kehuleixing").removeAttr("style");
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#kehuleixing").attr("style", "display:none");
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#kehuleixing").attr("style", "display:none");
        pingtai='0'
    }
}
var len
var common=79//要限制输入的字数
$('#commonText').text('('+common+')')
function changeDuankou() {

    $("#yidong").css('outline','')
    $("#pc").css('outline','')

    var title = $("#title").val()

    if ($("#yidong").is(":checked")&&!$("#pc").is(":checked")){
        duankou='20'

        common=79

        if (title.indexOf('_方盛云采')>=0){
            title = title.substr(0,common-9)+'_方盛云采'
        }else{
            title = title.substr(0,common)
        }
        $("#title").val(title)


    }
    if ($("#pc").is(":checked")&&!$("#yidong").is(":checked")){
        duankou='10'

        common=109

        if (title.indexOf('_方盛云采')<0){
            title = title.substr(0,common-9)+'_方盛云采'
        }else{
            title = title.substr(0,common)
        }

        $("#title").val(title)

    }
    if (!$("#pc").is(":checked")&&!$("#yidong").is(":checked")){
        duankou='0'
    }
    strlen(title)
    $('#commonText').text('('+common+')')
    console.log(duankou)

    var isApp=''
    if (duankou=='20'){
        isApp='&equipment=h5'
        $("#ordinary_url").val(root.sroreinter.ordinaryNofront+"Activity?activityId="+id+isApp);
        $("#contract_url").val(root.sroreinter.contractNofront+"Activity?activityId="+id+isApp);
    }else if (duankou=='10'){
        $("#ordinary_url").val(root.sroreinter.ActivityOrdinary+"Activity/"+id+".html"+isApp);
        $("#contract_url").val(root.sroreinter.Activitycontract+"Activity/"+id+".html"+isApp);
    }
}

function changeKehu() {
    $("#fen").css('outline','')
    $("#nofen").css('outline','')

    if ($("#fen").is(":checked")&&$("#nofen").is(":checked")){
        kehu='10'+","+'20'
    }
    if ($("#fen").is(":checked")&&!$("#nofen").is(":checked")){
        kehu='10'
    }
    if ($("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='20'
    }
    if (!$("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='0'
    }
    console.log(kehu)
}


//限制字符
var zifushu = 0
var Preser
function checkMaxIut(me) {
    var text=$(me).val()
    strlen(text)
    if(len>common){
        text=text.substr(0,zifushu)

        $(me).val(text)
    }else {
        Preser=$(me).val()
    }
}
function strlen(str){
    len = 0;
    var cishu=0
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        } else {
            len+=2;
        }
        if (len>common){
            if (cishu==0){
                zifushu=i
                cishu=1
            }
        }
    }
    $('#textSpan').text((len>common)?common:len)
    return len;
}

function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}